//
// Created by fh on 2021/8/6.
//

#include "CArray.h"
#include <time.h>

/* 计算运行时间 */
void calculateRuntime() {
    long    i = 10000000L;
    clock_t start, finish;
    double  duration;
    /* 测量一个事件持续的时间*/
    printf( "Time to do %ld empty loops is ", i );
    start = clock();
    while( i-- )      ;
    finish = clock();
    duration = (double)(finish - start) / CLOCKS_PER_SEC;
    printf( "%f seconds\n", duration );
}

/* 初始化可变数组 */
void allocInit(CArray **array) {
    CArray *list = (CArray *) malloc(sizeof(CArray));
    list->a = (int *)malloc(sizeof(int) * EACHSIZE);
    list->capacity = EACHSIZE;
    list->length = 0;
    *array = list;
}

/* 释放可变数组 */
void release(CArray **array) {
    if ((*array) == NULL) {
        return;
    }
    free((*array)->a);
    (*array)->a = NULL;
    free(*array);
    *array = NULL;
}

/* 添加元素 */
void add(CArray *array,int data) {
    if (array->length % EACHSIZE == 0 && array->length != 0) {
        // 需要扩容
        array->a = realloc(array->a,array->length + EACHSIZE);
        array->capacity = array->length + EACHSIZE;
    }
    array->a[array->length] = data;
    array->length++;
}

/* 删除元素 */
void removeElementByIndex(CArray *array,int index) {
    if (index >= array->length) {
        printf("数组下标越界了!!!\n");
        return;
    }
    for (int i = index; i < array->length - 1; ++i) {
        array->a[i] = array->a[i+1];
    }
    array->length--;
    // 缩容
    int tmp = array->capacity - array->length;
    if (tmp > 2 * EACHSIZE) {
        array->a = realloc(array->a,array->capacity - EACHSIZE);
        array->capacity = array->capacity - EACHSIZE;
    }
}

/* 替换元素 */
void replaceElementByIndex(CArray *array,int replaceIndex,int replaceData) {
    if (replaceIndex >= array->length) {
        printf("数组下标越界了!!!\n");
        return;
    }
    if (array != NULL) {
        array->a[replaceIndex] = replaceData;
    }
}

/* 查找元素 */
void getElementByIndex(CArray *array,int index,int *element) {
    if (index >= array->length) {
        printf("数组下标越界了!!!\n");
        return;
    }
    if (array != NULL) {
        *element = array->a[index];
    }
}

/* 打印数组内容 */
void display(CArray *array) {
    printf("<--------- display start --------\n");
    for (int i = 0; i < array->length; ++i) {
        printf("[%d]:%d\n",i,(array->a)[i]);
    }
    printf("---------- display end --------->\n");
}


/* 测试demo */
void arrayDemo() {
    clock_t start, finish;
    double  duration;
    start = clock();


    CArray *array = NULL;
    allocInit(&array);

    int t = 7;
    add(array,t);
    add(array,5);
    add(array,125);
    add(array,25);
    add(array,45);

    int get;
    getElementByIndex(array,3,&get);
    printf("%d\n",get);

    replaceElementByIndex(array,2,888888);
    display(array);

    removeElementByIndex(array,2);
    display(array);

    add(array,99);
    display(array);

    for (int i = 0; i < 30; ++i) {
        add(array,i);
    }
    display(array);

    removeElementByIndex(array,2);
    display(array);

    for (int i = 0; i < 30; ++i) {
        replaceElementByIndex(array,6,i);
    }
    display(array);

    for (int i = 0; i < 30; ++i) {
        removeElementByIndex(array,0);
    }
    display(array);

    release(&array);

    time_t a = 0;
    printf("%d\n", time(&a));

    double ccc = difftime(time(&a),a);
    printf("%f\n",ccc);

    finish = clock();
    duration = (double)(finish - start) / CLOCKS_PER_SEC;
    printf( "%f seconds\n", duration );


}